Conditions | 11 |
Paths | 7 |
Total Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like isAuthorized.js ➔ middleware often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import { |
||
6 | var middleware = function(req, res, next) { |
||
7 | if (!config.users.enable) { |
||
8 | if (req.url.indexOf('/abe/users/login') > -1) { |
||
9 | res.redirect('/abe/editor') |
||
10 | return |
||
11 | }else { |
||
12 | next() |
||
13 | return |
||
14 | } |
||
15 | } |
||
16 | |||
17 | var decoded = User.utils.decodeUser(req, res) |
||
18 | var user = User.utils.findSync(decoded.iss) |
||
19 | res.user = user |
||
20 | |||
21 | if(!User.utils.isAbeRestrictedUrl(req.url)) { |
||
22 | if (user != null && req.url.indexOf('/abe/users/login') > -1 && req.method === 'GET' ) { |
||
23 | res.redirect('/abe/editor') |
||
24 | return |
||
25 | }else { |
||
26 | next() |
||
27 | return |
||
28 | } |
||
29 | } |
||
30 | |||
31 | var isHtml = /text\/html/.test(req.get('accept')) ? true : false |
||
32 | |||
33 | if (user != null && User.utils.isUserAllowedOnRoute(user.role.workflow, req.url)) { |
||
34 | next() |
||
35 | }else { |
||
36 | if(isHtml) { |
||
37 | res.redirect('/abe/users/login') |
||
38 | }else { |
||
39 | var notAuthorized = { |
||
40 | success: 0, |
||
41 | message: 'Not authorized !' |
||
42 | } |
||
43 | res.set('Content-Type', 'application/json') |
||
44 | res.send(JSON.stringify(notAuthorized)) |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 | |||
49 | export default middleware |